package com.fengwan.wastesort.ui.viewModel

import android.app.Application
import android.database.sqlite.SQLiteDatabase
import androidx.lifecycle.AndroidViewModel
import com.fengwan.waste.util.writeTo
import java.io.File
import java.io.InputStream

/**
 * @author : Silence丶J
 * @time : 2019/7/18
 * @description :
 */
open class WasteDbViewModel(val app: Application) : AndroidViewModel(app) {

    private val dbPath = "${app.filesDir.absolutePath}/waste.db"
    protected val tabName = "waste_list"

    protected val database: SQLiteDatabase by lazy {
        val dbFile = File(dbPath)
        if (!dbFile.exists()) {
            val inputStream: InputStream = app.assets.open("waste.db")
            inputStream.use { input ->
                dbFile.outputStream().use { output ->
                    input.writeTo(output, DEFAULT_BUFFER_SIZE)
                }
            }
        }
        return@lazy SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READONLY)
    }


}